<?php
$pageTitle = "首页";
include 'header.php';

$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$perPage = 50;
$offset = ($page - 1) * $perPage;

// 获取搜索关键词
$search_title = isset($_GET['title']) ? trim($_GET['title']) : '';

// 构建查询语句
if (!empty($search_title)) {
    $stmt = $pdo->prepare("SELECT COUNT(*) AS total FROM trainings WHERE title LIKE ?");
    $stmt->execute(["%$search_title%"]);
} else {
    $stmt = $pdo->prepare("SELECT COUNT(*) AS total FROM trainings");
    $stmt->execute();
}
$total = $stmt->fetchColumn();

if (!empty($search_title)) {
    $stmt = $pdo->prepare("SELECT id, title, start_time, end_time, location, department, type FROM trainings WHERE title LIKE ? ORDER BY created_at DESC LIMIT $offset, $perPage");
    $stmt->execute(["%$search_title%"]);
} else {
    $stmt = $pdo->prepare("SELECT id, title, start_time, end_time, location, department, type FROM trainings ORDER BY created_at DESC LIMIT $offset, $perPage");
    $stmt->execute();
}
$trainings = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="row">
    <div class="col-md-12">


        <?php if ($total > 0) { ?>
            <h2>培训信息列表</h2>
            <!-- 搜索表单 -->
            <div class="mb-3">
                <form method="get" action="index.php" class="form-inline">
                    <div class="input-group">
                        <input type="text" name="title" class="form-control" placeholder="按主题搜索" value="<?= htmlspecialchars($search_title) ?>">
                        <button class="btn btn-outline-secondary" type="submit">搜索</button>
                        <?php if (!empty($search_title)): ?>
                            <a href="index.php" class="btn btn-outline-secondary">清除</a>
                        <?php endif; ?>
                    </div>
                </form>
            </div>
            <div class="table-responsive">

                <table class="table table-striped table-hover table-bordered">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>主题</th>
                            <th>时间</th>
                            <th>地点</th>
                            <th>主办科室或学校</th>
                            <th>任务或自主</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($trainings as $training): ?>
                            <tr>
                                <td><?= htmlspecialchars($training['id']) ?></td>
                                <td><?= htmlspecialchars($training['title']) ?></td>
                                <td><?= htmlspecialchars($training['start_time'] . ' 至 ' . $training['end_time']) ?></td>
                                <td><?= htmlspecialchars($training['location']) ?></td>
                                <td><?= htmlspecialchars($training['department']) ?></td>
                                <td><?= htmlspecialchars($training['type']) ?></td>
                                <td>
                                    <a href="training_info.php?id=<?php echo $training['id']; ?>"
                                        class="btn btn-sm btn-info mr-1">详情</a>
                                    <a href="teacher_training_registration.php?id=<?php echo $training['id']; ?>"
                                        class="btn btn-sm btn-warning mr-1">报名</a>
                                    <a href="cost_edit.php?id=<?php echo $training['id']; ?>"
                                        class="btn btn-sm btn-warning mr-1">费用填报</a>
                                    <?php if (isset($_SESSION['username']) && $_SESSION['username'] == 'admin') { ?>
                                        <a href="cost_list.php?id=<?php echo $training['id']; ?>"
                                            class="btn btn-sm btn-warning mr-1">费用列表</a>
                                        <a href="training_edit.php?id=<?php echo $training['id']; ?>"
                                            class="btn btn-sm btn-warning mr-1">修改</a>
                                        <a href="training_delete.php?id=<?php echo $training['id']; ?>"
                                            class="btn btn-sm btn-warning mr-1">删除</a>
                                    <?php } ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <nav aria-label="Page navigation">
                <ul class="pagination" id="pagination">
                    <?php for ($i = 1; $i <= ceil($total / $perPage); $i++) { ?>
                        <li class="page-item <?php echo ($i == $page) ? 'active' : ''; ?>">
                            <a class="page-link" href="?page=<?php echo $i; ?><?php if (!empty($search_title)) echo '&title=' . urlencode($search_title); ?>"><?php echo $i; ?></a>
                        </li>
                    <?php } ?>
                </ul>
            </nav>
        <?php } else { ?>
            <p class="text-center">暂无数据</p>
        <?php } ?>
    </div>
</div>
<?php include 'footer.php'; ?>